include( 'shared.lua' )

function ShowHelpMenu()
  local Help = vgui.Create( "DFrame" )
  Help:SetSize( 300, 350 )
  Help:SetTitle( "RaceGM - Rules & Help" )
  Help:SetVisible( true )
  Help:SetDraggable( true )
  Help:ShowCloseButton( true )
  Help:MakePopup()
  Help:Center()
 
  local Close = vgui.Create( "DButton" , Help)
  Close:SetText( "Close" )
  Close:SetPos( 25, 320 )
  Close:SetSize( 250, 25 )
  Close.DoClick = function () Help:Close() end
 
  HelpText= vgui.Create("DLabel", Help)
  HelpText:SetText("F1 - Enter Queue or Respawn.\nF2 - Open Game Menu.\nF3 - Open Shop Menu\nF4 - Open Rules & Help Menu\n\n1. You must race. No sitting idle.\n2. d[^_^]b is awesome.")
  HelpText:SizeToContents()
  HelpText:SetPos( 25, 35 )
end
usermessage.Hook( "call_vgui_helpmenu", ShowHelpMenu )

function ShowGameMenu()
  local GameMenu = vgui.Create( "DFrame" )
  GameMenu:SetSize( 600, 400 )
  GameMenu:SetTitle( "RaceGM - Game Menu" )
  GameMenu:SetVisible( true )
  GameMenu:SetDraggable( true )
  GameMenu:ShowCloseButton( true )
  GameMenu:MakePopup()
  GameMenu:Center()
 
  local Kill = vgui.Create( "DButton" , GameMenu)
  Kill:SetText( "Leave Race" )
  Kill:SetPos( 25, 35 )
  Kill:SetSize( 200, 50 )
  Kill.DoClick = function () RunConsoleCommand( "killSilent" ) end
 
end
usermessage.Hook( "call_vgui_game", ShowGameMenu )

function DrawHud()
        surface.SetDrawColor( 0, 0, 0, 255)
        surface.DrawRect(0 , ScrH()-70, 220, 50)
		surface.DrawRect(0, 0, 220, 40)
        surface.SetTextColor( 200, 200, 200, 255 )
        surface.SetTextPos( 0, ScrH()-40 )
        surface.SetFont("Trebuchet20")
		if(queueTime >= 0)then
			surface.DrawText(queueTime .." Seconds Until Next Race.")
		else
			surface.DrawText("Race Started.")
		end
		surface.SetTextPos( 0, ScrH()-60 )
        surface.DrawText("Lap(".. LocalPlayer():Frags() .."/ ".. lapText ..")")
		surface.SetDrawColor( 100, 100, 100, 255)
		surface.DrawRect(10, 10, 200, 20);
		surface.SetDrawColor( 255, 0, 0, 255)
		surface.DrawRect(10, 10, 200*(LocalPlayer():Health()/100), 20);
        for k, v in pairs(player.GetAll()) do
                if v:Nick() == LocalPlayer():Nick() then
                else
                        you = LocalPlayer():GetShootPos()
                        lookat = v:GetShootPos()
                        distance = you:Distance(lookat)
                        //Check if you can see this player
                                //We didn't hit the world, but the player!
                                local pos = v:GetShootPos():ToScreen()
                                //Credits to foszor for this line <img src="./images/smilies/icon_e_smile.gif" alt=":)" title="Smile" />
                                pos.y = pos.y - 75
                                pos.y = pos.y + (75 * (v:GetShootPos():Distance(LocalPlayer():GetShootPos()) / 2048)) * 0.5
                                transparency = 255 * (300/distance)
                                if transparency > 255 then
                                        transparency = 255
                                end
                                draw.DrawText( v:Nick() , "ScoreboardText", pos.x, pos.y, Color(255, 255, 255, transparency), 1)
                end
        end
        if !(timer.IsTimer("playerTime")) then
                timer.Create("playerTime", 0.5, 0, DrawHud)
        end
end
hook.Add("HUDPaint", "HUD_TEST", DrawHud)

function SetQ(um)
	queueTime = um:ReadLong()
end
usermessage.Hook("setQueueTime", SetQ)

function SetL(um)
	lapText = um:ReadLong()
end
usermessage.Hook("setLaps", SetL)

function GM:Initialize()
        queueTime = 0
        lapText = 0
end

function GM:DrawDeathNotice()
        return
end

function HideHud(name)
        for k, v in pairs{"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"} do
                if name == v then return false end
        end
end
hook.Add( "HUDShouldDraw", "hidehud", HideHud )